import requests
import json

import xlsxwriter as xw

def save(data, fileName):
    workbook = xw.Workbook(fileName)  # 创建工作簿
    worksheet1 = workbook.add_worksheet("sheet1")  # 创建子表
    worksheet1.activate()  # 激活表
    title = ['id', 'pid', 'name', 'order', 'display', 'remark']  # 设置表头
    worksheet1.write_row('A1', title)  # 从A1单元格开始写入表头
    i = 2  # 从第二行开始写入数据
    for j in range(len(data)):
        data[j]["remark"].strip()
        insertData = [data[j]["id"], data[j]["pid"], data[j]["name"], data[j]["order"], data[j]["display"], data[j]["remark"]]
        row = 'A' + str(i)
        worksheet1.write_row(row, insertData)
        i += 1
    workbook.close()


if __name__ == '__main__':
    url = 'https://www.honghaiwangluo.com/index/index/init_data'
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
    }
    response = requests.get(url=url,headers=header)
    dic_obj = response.json()
    # print(dic_obj['data']['taxonomy']['report_type'])
    data = dic_obj['data']['taxonomy']['report_type']
    save(data, 'data.xlsx')